package com.hyp.zxing;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.MultiFormatOneDReader;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * @author 何亚培
 * @version V1.0
 * @Date 2019/4/24 15:32
 * @Description: TODO 生成二维码
 */
public class CreateQRCode {
    public static void main(String[] args) {
        int width = 300;
        int height = 300;
        String format = "png";
        String content = "何亚培你真好";

        //定义二维码的参数
        HashMap hints = new HashMap();
        //二维码支持中文要求
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        //二维码边距
        hints.put(EncodeHintType.MARGIN, 2);

        //输出二维码
        try{
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            Path file = new File("d:/1.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix,format,file);
        }catch (Exception e){

        }

    }
}
